﻿Public Class frmManageUser_Form 
    Public is_edit As Boolean = False
    Public id As String = ""

    Private Sub frmManageUser_Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim mysql As New cMySQLDatabase
        If is_edit Then
            Me.Text = "Edit User"
            If mysql_isconnected() Then
                Dim data As DataTable
                data = mysql.mysql_query("SELECT * FROM tb_user WHERE id_user='" & id & "'")
                If data.Rows.Count > 0 Then
                    TextEditName.Text = data(0)("nama_user")
                    TextEditUsername.Text = data(0)("username")
                    Select Case data(0)("level").ToString
                        Case "0"
                            ComboBoxPermission.Text = "User"
                        Case "1"
                            ComboBoxPermission.Text = "Admin"
                        Case Else
                            ComboBoxPermission.Text = "User"
                    End Select
                Else
                    MessageBox.Show("Data Not Found. Please Try Again Later")
                    Me.Close()
                End If
            Else
                MessageBox.Show("Cannot Edit User Right Now. Please Try Again Later")
                Me.Close()
            End If
        Else
            TextEditUsername.Enabled = True
            Me.Text = "Add User"
        End If
    End Sub

    Private Sub SimpleButtonSave_Click(sender As Object, e As EventArgs) Handles SimpleButtonSave.Click
        Dim mysql As New cMySQLDatabase
        Dim obj As Object
        If is_edit Then
            If ComboBoxPermission.Text IsNot String.Empty And TextEditUsername.Text IsNot String.Empty And _
               TextEditName.Text IsNot String.Empty  Then
                obj = mysql.mysql_query("SELECT * FROM tb_user WHERE username='" & TextEditUsername.Text & "' AND id_user<>'" & id & "'")
                If Type.GetTypeCode(obj.GetType) <> TypeCode.Boolean Then
                    Dim data As DataTable = obj
                    Dim level As String
                    Select Case ComboBoxPermission.Text
                        Case "User"
                            level = "0"
                        Case "Admin"
                            level = "1"
                        Case Else
                            MessageBox.Show("Invalid Permission")
                            Exit Sub
                    End Select
                    If data.Rows.Count = 0 Then
                        If TextEditNewPassword.Text IsNot String.Empty Or TextEditCNewPassword.Text IsNot String.Empty Then
                            If TextEditNewPassword.Text.Equals(TextEditCNewPassword.Text) Then
                                If mysql.mysql_non_query(String.Format("UPDATE tb_user SET `password`=MD5('{0}'),`nama_user`='{1}',`level`='{2}' WHERE id_user='{3}'", TextEditNewPassword.Text, TextEditName.Text, level, id)) = False Then
                                    MessageBox.Show("Failed to update user. Please try again")
                                Else
                                    frmManageUser.loadUser()
                                    Me.Close()
                                End If
                            Else
                                MessageBox.Show("Password doesn't match with confirmation password")
                            End If
                        Else
                            If mysql.mysql_non_query(String.Format("UPDATE tb_user SET `nama_user`='{0}',`level`='{1}' WHERE id_user='{2}'", TextEditName.Text, level, id)) = False Then
                                MessageBox.Show("Failed to update user. Please try again")
                            Else
                                frmManageUser.loadUser()
                                Me.Close()
                            End If
                        End If
                        
                    Else
                        MessageBox.Show("Username is not available to use")
                    End If
                Else
                    MessageBox.Show("Failed to update new user. Please try again")
                End If
            Else
                MessageBox.Show("Please fill all required field")
            End If
        Else
            If ComboBoxPermission.Text IsNot String.Empty And TextEditUsername.Text IsNot String.Empty And _
               TextEditName.Text IsNot String.Empty And TextEditNewPassword.Text IsNot String.Empty And _
               TextEditCNewPassword IsNot String.Empty Then
                obj = mysql.mysql_query("SELECT * FROM tb_user WHERE username='" & TextEditUsername.Text & "'")
                If Type.GetTypeCode(obj.GetType) <> TypeCode.Boolean Then
                    Dim data As DataTable = obj
                    Dim level As String
                    Select Case ComboBoxPermission.Text
                        Case "User"
                            level = "0"
                        Case "Admin"
                            level = "1"
                        Case Else
                            MessageBox.Show("Invalid Permission")
                            Exit Sub
                    End Select
                    If data.Rows.Count = 0 Then
                        If TextEditNewPassword.Text.Equals(TextEditCNewPassword.Text) Then
                            If mysql.mysql_non_query(String.Format("INSERT INTO tb_user(`username`,`password`,`nama_user`,`level`) VALUES ('{0}',MD5('{1}'),'{2}','{3}')", TextEditUsername.Text, TextEditNewPassword.Text, TextEditName.Text, level)) = False Then
                                MessageBox.Show("Failed to add new user. Please try again")
                            Else
                                frmManageUser.loadUser()
                                Me.Close()
                            End If
                        Else
                            MessageBox.Show("Password doesn't match with confirmation password")
                        End If
                    Else
                        MessageBox.Show("Username is not available to use")
                    End If
                Else
                    MessageBox.Show("Failed to add new user. Please try again")
                End If
            Else
                MessageBox.Show("Please fill all required field")
            End If
        End If
    End Sub

    Private Sub SimpleButtonExit_Click(sender As Object, e As EventArgs) Handles SimpleButtonExit.Click
        Me.Close()
    End Sub

    
End Class